package com.dmxh.service.impl;

import com.dmxh.api.ItemAPI;
import com.dmxh.api.UserAPI;
import com.dmxh.pojo.TbItem;
import com.dmxh.pojo.TbUser;
import com.dmxh.service.CartService;
import com.dmxh.vo.CartlistVo;
import com.dmxh.vo.DetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserAPI userAPI;
    @Autowired
    private ItemAPI itemAPI;

    @Override
    public void addCart(String id, long itemId, Integer num) {
        //获取登陆用户的购物车列表信息
            //获取登陆用户
        TbUser tbUser = userAPI.queryUserById(Long.valueOf(id));
        List<CartlistVo> cartlistVoList = (List<CartlistVo>) redisTemplate.boundHashOps("cartList").get(tbUser.getUsername());
        //获取添加商品信息
        TbItem tbItem = itemAPI.queryById(itemId);
        if (cartlistVoList == null) {
            cartlistVoList = new ArrayList<CartlistVo>();
        }
        //判断购物车是否为空
        if (cartlistVoList.size()!=0) {
            //购物车不为空时,遍历购物车集合
            CartlistVo cart = null;
            for (CartlistVo cartlistVo : cartlistVoList) {
                //判断商家是否存在
                if (Objects.equals(cartlistVo.getSeller(), tbItem.getSeller())) {
                    //商家已存在
                    cart = cartlistVo;
                    break;
                }
            }
            if (cart!=null){
                //判断商品是否存在
                List<DetailVo> list = cart.getList();
                DetailVo detail = null;
                for (DetailVo detailVo : list) {
                    if (Objects.equals(detailVo.getItemId(), tbItem.getId())) {
                        //商品存在
                        detail = detailVo;
                        break;
                    }
                }
                if (detail == null) {
                    //商品没存过
                    detail = new DetailVo();
                    detail.setGoodsId(tbItem.getGoodsId());
                    detail.setItemId(tbItem.getId());
                    detail.setPrice(tbItem.getPrice());
                    detail.setTitle(tbItem.getTitle());
                    detail.setNum(num);
                    detail.setPicPath(tbItem.getImage());
                    detail.setTotalFee(tbItem.getPrice().multiply(new BigDecimal(detail.getNum())));
                    list.add(detail);
                } else {
                    //商品存过
                    detail.setNum(detail.getNum() + num);
                    detail.setTotalFee(detail.getPrice().multiply(new BigDecimal(detail.getNum())));
                    if (detail.getNum() <= 0) {
                        list.remove(detail);
                    }
                    if (cart.getList().size() == 0) {
                        cartlistVoList.remove(cart);
                    }
                }
            }else {
                //商家不存在
                CartlistVo newCartlistVo = new CartlistVo();
                ArrayList<DetailVo> detailVos = new ArrayList<>();
                DetailVo detailVo = new DetailVo();
                detailVo.setGoodsId(tbItem.getGoodsId());
                detailVo.setItemId(tbItem.getId());
                detailVo.setPrice(tbItem.getPrice());
                detailVo.setTitle(tbItem.getTitle());
                detailVo.setNum(num);
                detailVo.setPicPath(tbItem.getImage());
                detailVo.setTotalFee(tbItem.getPrice().multiply(new BigDecimal(detailVo.getNum())));
                detailVos.add(detailVo);
                newCartlistVo.setList(detailVos);
                newCartlistVo.setSeller(tbItem.getSeller());
                cartlistVoList.add(newCartlistVo);
            }
        } else {
            //购物车不存在
            CartlistVo newCartlistVo = new CartlistVo();
            ArrayList<DetailVo> detailVos = new ArrayList<>();
            DetailVo detailVo = new DetailVo();
            detailVo.setGoodsId(tbItem.getGoodsId());
            detailVo.setItemId(tbItem.getId());
            detailVo.setPrice(tbItem.getPrice());
            detailVo.setTitle(tbItem.getTitle());
            detailVo.setNum(num);
            detailVo.setPicPath(tbItem.getImage());
            detailVo.setTotalFee(tbItem.getPrice().multiply(new BigDecimal(detailVo.getNum())));
            detailVos.add(detailVo);
            newCartlistVo.setList(detailVos);
            newCartlistVo.setSeller(tbItem.getSeller());
            cartlistVoList.add(newCartlistVo);
        }
        //存入Redis
        redisTemplate.boundHashOps("cartList").put(tbUser.getUsername(),cartlistVoList);
    }

    @Override
    public List<CartlistVo> getCart(String id) {
        TbUser tbUser = userAPI.queryUserById(Long.valueOf(id));
        List<CartlistVo> cartList = (List<CartlistVo>) redisTemplate.boundHashOps("cartList").get(tbUser.getUsername());
        return cartList;
    }
}
